package io.sk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.sk.common.base.BaseResult;
import io.sk.common.utils.CheckDataUtils;
import io.sk.dao.PersonnelDao;
import io.sk.entity.PersonnelEntity;
import io.sk.entity.SysUserEntity;
import io.sk.service.PersonnelService;
import io.sk.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("personnelService")
public class PersonnelServiceImpl extends ServiceImpl<PersonnelDao, PersonnelEntity> implements PersonnelService {


    @Autowired
    private PersonnelDao personnelDao;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 人员注册信息验证
     *
     * @param personnelEntity 参数信息
     * @return BaseResult
     */
    @Override
    public BaseResult checkPersonnel(PersonnelEntity personnelEntity) {

        String middleStr;

        //手机号
        middleStr = CheckDataUtils.stringTrim(personnelEntity.getPhone());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("请输入手机号");
        }
        personnelEntity.setPhone(middleStr);
        //手机号验证
        SysUserEntity sysUser = sysUserService.selectOne(
                new EntityWrapper<SysUserEntity>().eq("phone", personnelEntity.getPhone())
        );
        if (null != sysUser) {
            return CheckDataUtils.sendResult601();
        }

        //姓名
        middleStr = CheckDataUtils.stringTrim(personnelEntity.getName());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("请输入姓名");
        }
        personnelEntity.setName(middleStr);

        return CheckDataUtils.sendResult200(personnelEntity);
    }

    /**
     * @param id          主键id
     * @param avatar      头像
     * @param certificate 教师资格证图片：逗号隔开
     * @return BaseResult
     */
    @Override
    public BaseResult certificate(String id, String avatar, String certificate) {

        //主键验证
        PersonnelEntity personnelEntity = personnelDao.selectById(id);
        if (null == personnelEntity) {
            return CheckDataUtils.sendResult602();
        }

        personnelEntity.preUpdate(id);
        personnelEntity.setAvatar(avatar);
        personnelEntity.setCertificate(certificate);

        Integer updateNum = personnelDao.updateById(personnelEntity);
        if (!CheckDataUtils.checkUpdateNum(updateNum)) {
            return CheckDataUtils.sendResult606();
        }

        return CheckDataUtils.sendResult200();
    }
}
